<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标准号" prop="standardCode">
        <el-input
          v-model="queryParams.standardCode"
          placeholder="请输入标准号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['stdspider:spider:add']"
        >新增采集</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['stdspider:spider:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['stdspider:spider:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stdspider:spider:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >刷新</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table  v-loading="loading" :data="spiderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="标准号" align="center" prop="standardCode" />
      <el-table-column
        label=" "
        align="center"
        prop="a11">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="a11" placement="top">
            <span>参与国家影响力</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label=" " align="center" prop="a12">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="a12" placement="top">
            <span>参与企业/组织影响力</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="a13">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="a13" placement="top">
            <span>起草人员</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b11">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b11" placement="top">
            <span>标准转化</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b12">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b12" placement="top">
            <span>其他标准引用</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b13">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b13" placement="top">
            <span>被法律法规引用</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b14">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b14" placement="top">
            <span>其他工程应用</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b21">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b21" placement="top">
            <span>标准传播</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b22">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b22" placement="top">
            <span>认证认可</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b23">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b23" placement="top">
            <span>标准关注度</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b24">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b24" placement="top">
            <span>标准衍生材料传播</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b31">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b31" placement="top">
            <span>标准引领性</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b32">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b32" placement="top">
            <span>标准创新性</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b33">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b33" placement="top">
            <span>标准获奖</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b41">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b41" placement="top">
            <span>社会关注度</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b42">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b42" placement="top">
            <span>可持续发展支撑度</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b43">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b43" placement="top">
            <span>对外贸易应用</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label=" " align="center" prop="b44">
        <template slot="header" slot-scope="scope">
          <el-tooltip content="b44" placement="top">
            <span>产业扩散广度</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status" fixed="right" >
        <template slot-scope="scope">
          <el-tag :type="statusTagMap[scope.row.status]">{{ statusMap[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column fixed="right" width="120" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row)"
            v-hasPermi="['stdspider:spider:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['stdspider:spider:remove']"
          >删除数据</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核对话框 -->
    <el-dialog class="custom-dialog"  :title="title" :visible.sync="openAudit" width="75%" append-to-body>
<!--      <el-form ref="form" :model="form" :rules="rulesAudit" label-width="200px">-->
        <!--        <el-form-item label="标准号" prop="standardCode">-->
        <!--          <el-input v-model="form.standardCode" placeholder="修改值标准号" />-->
        <!--        </el-form-item>-->
          <el-radio-group v-model="auditStatus">
            <el-radio :label="1" >通过</el-radio>
            <el-radio :label="2" >不通过</el-radio>
          </el-radio-group>
      <el-table  :data="tableData" fit height="640"  style="width: 100%">
        <el-table-column width="200" align="center" prop="label" label="指标"></el-table-column>
        <el-table-column width="120" align="center" prop="oldValue" label="X当前采集值">
        </el-table-column>
        <el-table-column width="300" align="center" label="修改审核数据X">
          <template slot-scope="scope">
            <el-input-number
              v-model="scope.row.value"
              :min="0"
              :max="10000"
              label="修改值">
            </el-input-number>
          </template>
        </el-table-column>
          <el-table-column label="计算方式" align="center"  :show-overflow-tooltip="true">
            <template #default="scope">
              <div v-katex:auto >{{ scope.row.calculation }}</div>
            </template>
          </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAuditForm">确 定</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 爬虫复选框 -->
    <el-dialog class="custom-dialog"  :title="spiderTitle" :visible.sync="spiderOpen" width="80%" append-to-body>

      <el-container  style="border-bottom: deepskyblue solid 1px; width: 100%; height: 80%">
        <el-aside width="20%" style="background-color: rgb(238, 241, 246)">
          <el-form ref="spiderForm" :rules="spiderRules" :model="spiderForm" >
            <el-checkbox v-model="allSelected" @change="handleCheckAll">全选</el-checkbox>
            <el-form-item v-for="item in checkboxItems" :key="item.label">
              <el-checkbox v-model="spiderForm[item.value]" :label="item.label">{{ item.label }}({{ item.description }})</el-checkbox>
            </el-form-item>
          </el-form>
        </el-aside>
        <!-- Add more fields as needed -->
        <!-- Dialog Footer -->
        <el-container class="conmain">
          <el-header style="text-align: left; font-size: 12px">
            <el-form :model="queryStandParams" ref="queryStandForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="标准号" prop="standardCode">
                <el-input
                  v-model="queryStandParams.standardCode"
                  placeholder="请输入标准号"
                  clearable
                  @keyup.enter.native="handleStandQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleStandQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetStandQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </el-header>
          <el-main >
            <el-table
              v-loading="standloading"
              :data="standardCodeList"
              @selection-change="handleStdSelectionChange"
              height="100%"
            >
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="标准号" align="center" prop="standardCode" />
            </el-table>
            <div>总条目数: {{ standardCodeList.length }}</div>
          </el-main>
        </el-container>
      </el-container>

      <div slot="footer" class="dialog-footer" >
        <el-button type="primary" @click="beginSpider">确 定</el-button>
        <el-button @click="cancelSpider">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSpider,
  getSpider,
  delSpider,
  addSpider,
  updateSpider,
  startSpider,
  auditSpider, getStandardList
} from "@/api/stdspider/spider";
import {getAllStandardCode} from "@/api/standard/standard";
import {listExcel} from "@/api/evaluate/startEvaluate";
import 'katex/dist/katex.min.css';
import katex from 'katex/dist/katex.min'
import 'katex/dist/contrib/auto-render.min'
import VueKatex from 'vue-katex';
import renderMathInElement from "katex/contrib/auto-render/auto-render";


const statusMap = {
  0: "待审核",
  1: "通过审核",
  2: "未通过审核"
};
const statusTagMap = {
  0: "warning",
  1: "success",
  2: "danger"
};
export default {
  name: "Spider",
  data() {
    return {
      // 遮罩层
      loading: true,
      standloading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      standIds: [],
      selectStandardCodes:[],
      standSingle: true,
      standMultiple: true,
      // 显示搜索条件
      showSearch: true,
      selectedFields:[],
      statusMap,
      statusTagMap,
      // 总条数
      total: 0,
      // 表格数据
      spiderList: [],
      standardCodeList:[],
      // 弹出层标题
      title: "",
      spiderTitle:"",
      // 是否显示弹出层
      open: false,
      openAudit:false,
      spiderOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        standardCode: null,
      },
      queryStandParams:{
        pageNum: 1,
        pageSize: 10,
        standardCode: null,
      },
      // 表单参数
      form: {},
      auditStatus:0,
      // spiderForm:{},
      spiderForm: {
        a11: false,
        a12: false,
        a13: false,
        b11: false,
        b12: false,
        b13: false,
        b14: false,
        b21: false,
        b22: false,
        b23: false,
        b24: false,
        b31: false,
        b32: false,
        b33: false,
        b41: false,
        b42: false,
        b43: false,
        b44: false
      },
      checkboxItems: [
        { label: 'a11', value: 'a11', description: '参与国家影响力' },
        { label: 'a12', value: 'a12', description: '参与企业/组织影响力' },
        { label: 'a13', value: 'a13', description: '起草人员' },
        { label: 'b11', value: 'b11', description: '标准转化' },
        { label: 'b12', value: 'b12', description: '其他标准引用' },
        { label: 'b13', value: 'b13', description: '被法律法规引用' },
        { label: 'b14', value: 'b14', description: '其他工程应用' },
        { label: 'b21', value: 'b21', description: '标准传播' },
        { label: 'b22', value: 'b22', description: '认证认可' },
        { label: 'b23', value: 'b23', description: '标准关注度' },
        { label: 'b24', value: 'b24', description: '标准衍生材料传播' },
        { label: 'b31', value: 'b31', description: '标准引领性' },
        { label: 'b32', value: 'b32', description: '标准创新性' },
        { label: 'b33', value: 'b33', description: '标准获奖' },
        { label: 'b41', value: 'b41', description: '社会关注度' },
        { label: 'b42', value: 'b42', description: '可持续发展支撑度' },
        { label: 'b43', value: 'b43', description: '对外贸易应用' },
        { label: 'b44', value: 'b44', description: '产业扩散度' }
      ],
      allSelected: false, // 全选状态
      tableData: [
        { label: 'a11(参与国家影响力)',oldValue:'', value: '',
          calculation: '\\( y = \\sum x_i \\) “x为各国竞争力数值，y为打分，最终结果归一化，在[0,1]之间”' ,值说明:'x为各国竞争力数值'},
        { label: 'a12(参与企业/组织影响力)',oldValue:'', value:'',
          calculation: '\\( y = \\frac{x}{5} \\)  “工作组成员所在企业位列世界500强的数量。每个得1分，最高可以获得5分，x为数量，y为得分”' },
        { label: 'a13(起草人员)',oldValue:'', value: '',
          calculation: '优:1.0  良:0.8  一般:0.5' },
        { label: 'b11(标准转化)',oldValue:'', value: '',
          calculation: '\\( \n' + 'y = \\begin{cases} \n' + '1, & \\text{if } x \\geq 10 \\\\ \n' + '\\frac{x}{10}, & \\text{if } x < 10 \n' + '\\end{cases} \\) “x为转化数量，y为得分”' },
        { label: 'b12(其他标准引用)',oldValue:'', value: '',
          calculation: '\\( \n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \\) “x为引用次数，y为得分”' },
        { label: 'b13(被法律法规引用)',oldValue:'', value: '',
          calculation: '暂无' },
        { label: 'b14(其他工程应用)',oldValue:'', value: '',
          calculation: '暂无' },
        { label: 'b21(标准传播)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \\)  “x为销售量，y为得分”' },
        { label: 'b22(认证认可)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & x = \\text{国际级别} \\\\ \n' + '0.8, & x = \\text{区域级别} \\\\ \n' + '0.5, & x = \\text{国家级别} \\\\ \n' + '0.3, & x = \\text{团体（其他）级别} \n' + '\\end{cases} \n' + '\\)  “x为认证类型，y为得分”' },
        { label: 'b23(标准关注度)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \n' + '\\)  ”x 为点击量，y 为得分“' },
        { label: 'b24(标准衍生材料传播)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \n' + '\\) “x 为衍生材料传播情况，y 为得分”' },
        { label: 'b31(标准引领性)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \n' + '\\) “x 为标准必要专利，y 为得分”' },
        { label: 'b32(标准创新性)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \n' + '\\) "x 为次数，y 为得分"' },
        { label: 'b33(标准获奖)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & x = \\text{国际或国家级别} \\\\ \n' + '0.5, & x = \\text{省部级别} \\\\ \n' + '0.2, & x = \\text{地方级别} \n' + '\\end{cases} \n' + '\\) ”根据不同级别的奖项设定不同的分值：x 为获奖级别，y 为得分“' },
        { label: 'b41(社会关注度)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \n' + '\\) “将根据报道次数和分值关系进行评定：x 为报道次数，y 为得分”' },
        { label: 'b42(可持续发展支撑度)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \n' + '\\) ”将根据关联次数和分值关系进行评定：x 为关联次数，y 为得分“' },
        { label: 'b43(对外贸易应用)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & \\text{如果在TBT和SPS中引用} \\\\ \n' + '0.0, & \\text{如果不在TBT和SPS中引用} \n' + '\\end{cases} \n' + '\\) ”① 如果在TBT和SPS中引用，则得1.0分；② 如果不在TBT和SPS中引用，则得0.0分“' },
        { label: 'b44(产业扩散广度)',oldValue:'', value: '',
          calculation: '\\(\n' + 'y = \\begin{cases} \n' + '1.0, & a_1 \\leq x < a_2 \\\\ \n' + '0.8, & a_2 \\leq x < a_3 \\\\ \n' + '\\vdots & \\vdots \n' + '\\end{cases} \n' + '\\) ”按区间打分：x 为被引用次数，y 为得分“' }
      ],
      // 表单校验
      rules: {
      },
      rulesAudit:{
      },
      spiderRules:{
      },
      /** SSE 订阅*/
      eventSource:null
    };
  },
  created() {
    this.getList();
    // this.getExcelData();
    // this.getStandardCodeList();
  },
  mounted() {
    this.getStandData();
  },
  watch: {
    // 监听全选状态变化
    allSelected(newVal) {
      for (const key in this.spiderForm) {
        this.spiderForm[key] = newVal;
      }
    },
    // 监听单个复选框变化
    'spiderForm': {
      handler(newVal) {
        let all = true;
        for (const key in newVal) {
          if (!newVal[key]) {
            all = false;
            break;
          }
        }
        this.allSelected = all;
      },
      deep: true // 深度监听
    }
  },
  methods: {
    subscribeToSSE() {
      this.eventSource = new EventSource('http://localhost:8181/sse/subscribe');

      this.eventSource.onopen = () => {
        console.log("SSE: 连接已建立");
      };

      this.eventSource.onmessage = (event) => {
        console.log('SSE: 收到消息: ', event.data);
        this.$message.success(event.data);

        // 收到消息后关闭连接
        this.eventSource.close();
        this.eventSource = null;
      };

      this.eventSource.onerror = (error) => {
        console.error('SSE: 连接有误,', error);
        this.$message.warning("SSE 订阅失败，请联系管理员");
        this.eventSource.close();
        this.eventSource = null;
      };
    },

    handleCheckAll(value) {
      for (const key in this.spiderForm) {
        this.spiderForm[key] = value;
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listSpider(this.queryParams).then(response => {
        this.spiderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getStandData() {
      this.standloading = true;
      getStandardList(this.queryStandParams).then(response => {
        console.log(response);
        this.standardCodeList = response.rows;
        this.standtotal = response.total;
        this.standloading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelAudit() {
      this.openAudit = false;
      this.reset();
    },
    cancelSpider() {
      this.spiderOpen = false;
      // this.reset();
      this.resetSpider();
    },
    resetSpider() {
      this.spiderForm = {
        standardCodes: null,
        a11: null,
        a12: null,
        a13: null,
        b11: null,
        b12: null,
        b13: null,
        b14: null,
        b21: null,
        b22: null,
        b23: null,
        b24: null,
        b31: null,
        b32: null,
        b33: null,
        b41: null,
        b42: null,
        b43: null,
        b44: null
      };
      this.resetForm("spiderForm");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        standardCode: null,
        a11: null,
        a12: null,
        a13: null,
        b11: null,
        b12: null,
        b13: null,
        b14: null,
        b21: null,
        b22: null,
        b23: null,
        b24: null,
        b31: null,
        b32: null,
        b33: null,
        b41: null,
        b42: null,
        b43: null,
        b44: null,
        status: null
      };
      this.tableData.forEach(item => {
        item.value='';
      });
      this.auditStatus =0;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 打开弹出层
    handleStandQuery() {
      this.queryStandParams.pageNum = 1;
      this.getStandData();
    },
    /** 重置按钮操作 */
    resetStandQuery() {
      this.resetForm("queryStandForm");
      this.handleStandQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleStdSelectionChange(selection) {
      this.standIds = selection.map(item => item.id)
      this.selectStandardCodes= selection.map(item => item.standardCode)
      this.standSingle = selection.length!==1
      this.standMultiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      this.spiderOpen = true;
      this.spiderTitle = "新增自动采集";
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //
    //   this.reset();
    //   const id = row.id || this.ids
    //   getSpider(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     //过滤form中的statu字段
    //     // this.form=this.form.filter(item => item !== "status");
    //     this.title = "修改采集数据";
    //   });
    // },
    /** 审核按钮操作 */
    handleCheck(row) {
      this.reset();
      const id = row.id || this.ids
      this.form.id=id
      this.form.standardCode=row.standardCode
      getSpider(id).then(response => {
        // console.log(this.form)
        if(response.data.status!=0){
          this.$modal.msgWarning("该数据已经审核")
        }
         else{
          this.tableData.forEach(item => {
            // 提取 label 中的键名，假设格式为 'a11'，并且键名总是 label 的最后一部分
            const key = item.label.match(/^[A-Za-z0-9]{3}/);
            // console.log(key)
            // 如果 response.data 包含这个键，更新 value
            if (response.data[key] !== undefined) {
              item.oldValue = response.data[key];
              if(item.oldValue>=0){
                item.value =item.oldValue;
              }
            }
          });
          this.openAudit = true;
          this.title = "审核采集数据";
        }
      });
    },
    validateInteger(row, event) {
      const value = event.target.value;
      const intValue = Number(value);
      if (intValue !== NaN && intValue === parseInt(intValue, 10)) {
        row.value = intValue;
      } else {
        // 如果输入的不是整数，重置为上一个有效值
        this.$message.error('请输入一个整数');
      }
    },
    submitAuditForm(){
      this.form.status = this.auditStatus;
      var tag=1;
      this.tableData.forEach(item => {
        // 提取 label 中的键名，假设格式为 'a11'，并且键名总是 label 的最后一部分
        const key = item.label.match(/^[A-Za-z0-9]{3}/);
        // console.log(key)
        // 如果 response.data 包含这个键，更新 value
          this.form[key]=item.value;
          if(this.form[key]<0){
            tag=0;
          }
      });
      console.log(this.tableData)
      console.log(this.form)
        if(tag===0){
          this.$modal.msgWarning("请补充数据");
        }
        else{
          if (this.auditStatus==1) {
            auditSpider(this.form).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.openAudit = false;
              this.getList();
            });
          }
          else if(this.auditStatus==0) {
            this.$modal.msgWarning("请选择审核状态");
          }
          else {
            this.$modal.msgWarning("审核成功");
          }
        }
    },
    beginSpider() {
      const standardCodes = this.selectStandardCodes;
      const params = Object.keys(this.spiderForm).filter(
        (key) => key !== "standardCodes" && this.spiderForm[key] ==true
      );
        if (this.selectStandardCodes.length == 0) {
          this.$modal.msgWarning("请选择至少一个标准进行数据采集");
        }
        else if(params.length==0) {
          this.$modal.msgWarning("请选择至少一个指标进行数据采集");
        }
        else{
          const requestData = {
            standardCodes: standardCodes,
            params: params,
          };
          // 开始计算前进行 SSE 订阅
          this.subscribeToSSE();
          // 发送请求启动任务
          startSpider(requestData).then((response) => {
            console.log(response)
            if(response.code===200){
              this.$message.success(response.msg);
            }else{
              this.$message.error(response.msg);
            }
            this.spiderOpen = false;
          }).catch(() => {
            this.$message.error('计算失败');
          });
        }
    },

    //获取所有的标准号
    // getStandardCodeList(){
    //   getAllStandardCode().then(response => {
    //     this.standardCodeList = response.data;
    //
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除序号为"' + ids + '"的数据项？').then(function() {
        return delSpider(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('stdspider/spider/export', {
        ...this.queryParams
      }, `spider_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
// ::v-deep.custom-dialog .el-dialog__body {
//}
 .dialog-footer {
   text-align: center;
 }
 .el-header {
   padding: 0;
 }
 .el-main {
   margin-top: -20px;
 }
 .conmain{
   overflow-y: auto;
   max-height: 500px;
   //background-color: #f5f7fa;
   //padding:0 20px;
 }
 .el-aside {
   overflow-y: auto;
   max-height: 500px;
   /* 超出时允许垂直滚动 */
   color: #333;
 }
</style>
